package br.ufrn.btest.script;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import de.be4.classicalb.core.parser.BException;
import de.be4.classicalb.core.parser.BParser;
import de.be4.classicalb.core.parser.node.Start;

public class CreateXMLFileScript {
	private static String generateXML(File machine) {
		BParser parser = new BParser();
		XStream xstream = new XStream(new DomDriver());
		String xml = null;

		try {
			Start startNode = parser.parseFile(machine, false);
			xml = xstream.toXML(startNode);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BException e) {
			e.printStackTrace();
		}
		return xml;
	}
	
	public static void main(String[] args) {
		String machinePath = "machines/TestsForOp_addPaper_From_Paperround.mch";
		File machine = new File(machinePath);
		
		//System.out.println(generateXML(machine));
		
		String xmlFilePath = machine.getParent() + "/" + machine.getName().substring(0, machine.getName().length() - 4) + ".xml";
		File xmlFile = new File(xmlFilePath);
		try {
			xmlFile.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}

		FileWriter out;
		try {
			out = new FileWriter(xmlFile);
			PrintWriter writer = new PrintWriter(out);
			writer.write(generateXML(machine));
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println("xml file created!");
	}
}
